﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WechatUtility;

namespace WechatDAL
{
    public static partial class WechatAPI4Public
    {
        /// <summary>
        /// 公众号的TOKEN
        /// </summary>
        public static string Token { get { return WebConfigHelper.GetAppSet("Token"); } }
        /// <summary>
        /// 密钥
        /// </summary>
        internal static string AESKey { get { return WebConfigHelper.GetAppSet("AESKey"); } }
        /// <summary>
        /// 公众号的应用ID
        /// </summary>
        internal static string AppID { get { return WebConfigHelper.GetAppSet("AppID"); } }
        /// <summary>
        /// 公众号的应用密钥
        /// </summary>
        internal static string AppSecret { get { return WebConfigHelper.GetAppSet("AppSecret"); } }

        private static string _oAuth = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri={0}&response_type=code&scope={1}{2}#wechat_redirect";
        /// <summary>
        /// 获取网页授权页面
        /// </summary>
        public static string OAuth
        {
            get { return WechatAPI4Public._oAuth.Replace("=APPID", "=" + AppID); }
        }

        private static string _oAuthToken = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code={0}&grant_type=authorization_code";
        /// <summary>
        /// 获得网页授权Token
        /// </summary>
        public static string OAuthToken
        {
            get { return WechatAPI4Public._oAuthToken.Replace("=APPID", "=" + AppID).Replace("=SECRET", "=" + AppSecret); }
        }

        private static string _getWechatIP = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 获取微信服务器IP
        /// </summary>
        public static string GetWechatIP
        {
            get { return _getWechatIP.SetToken(); }
        }

        private static string _getJSTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
        /// <summary>
        /// 获取JS接口ticket
        /// </summary>
        public static string GetJSTicket
        {
            get { return _getJSTicket.SetToken(); }
        }

        #region 上传下载接口
        private static string _uploadFile = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
        /// <summary>
        /// 上传媒体文件
        /// </summary>
        public static string UploadFile
        {
            get { return _uploadFile.SetToken(); }
        }

        private static string _downFile_get = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
        /// <summary>
        /// 下载媒体文件
        /// </summary>
        public static string DownFile
        {
            get { return _downFile_get.SetToken(); }
        }
        #endregion

        #region 客服接口
        private static string _addCustomService = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 添加客服
        /// </summary>
        public static string AddCustomService
        {
            get { return _addCustomService.SetToken(); }
        }

        private static string _updateCustomService_post = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 修改客服信息
        /// </summary>
        public static string UpdateCustomService
        {
            get { return _updateCustomService_post.SetToken(); }
        }

        private static string _deleteCustomService = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 删除客服
        /// </summary>
        public static string DeleteCustomService
        {
            get { return _deleteCustomService.SetToken(); }
        }

        private static string _setCustomServicePhoto = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT";
        /// <summary>
        /// 设置客服头像
        /// </summary>
        public static string SetCustomServicePhoto
        {
            get { return _setCustomServicePhoto.SetToken(); }
        }

        private static string _getAllCustomService = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 获取所有客服
        /// </summary>
        public static string GetAllCustomService
        {
            get { return _getAllCustomService.SetToken(); }
        }
        #endregion

        #region 消息接口
        private static string _sendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 向用户发送消息
        /// </summary>
        public static string Sendmessage
        {
            get { return _sendMessage.SetToken(); }
        }

        private static string _sendTempMessage = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 向用户发送模版消息
        /// </summary>
        public static string SendTempMessage
        {
            get
            {
                return _sendTempMessage.SetToken();
            }
        }
        #endregion

        #region 分组接口
        private static string _addGroup = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 添加分组
        /// </summary>
        public static string AddGroup
        {
            get { return _addGroup.SetToken(); }
        }

        private static string _getAllGroup = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 获取所有分组
        /// </summary>
        public static string GetAllGroup
        {
            get { return _getAllGroup.SetToken(); }
        }

        private static string _selectGroupIn = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 查询用户所在分组信息
        /// </summary>
        public static string SelectGroupIn
        {
            get { return _selectGroupIn.SetToken(); }
        }

        private static string _updateGroupName = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 修改分组名称
        /// </summary>
        public static string UpdateGroupName
        {
            get { return _updateGroupName.SetToken(); }
        }

        private static string _moveGroup = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 移动用户至某分组
        /// </summary>
        public static string MoveGroup
        {
            get { return _moveGroup.SetToken(); }
        }

        private static string _moveGroups = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 移动多个用户至某分组
        /// </summary>
        public static string MoveGroups
        {
            get { return _moveGroups.SetToken(); }
        }
        #endregion

        #region 用户信息接口
        private static string _setCustomMark = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 对用户设置备注名
        /// </summary>
        public static string SetCustomMark
        {
            get { return _setCustomMark.SetToken(); }
        }

        private static string _getCustomInfo = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        /// <summary>
        /// 获取用户基本信息
        /// </summary>
        public static string GetCustomInfo
        {
            get { return _getCustomInfo.SetToken(); }
        }

        private static string _getCustomList = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
        /// <summary>
        /// 获取用户列表（已关注用户列表）
        /// </summary>
        public static string GetCustomList
        {
            get { return _getCustomList.SetToken(); }
        }
        #endregion

        #region 菜单接口
        private static string _buildMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 创建菜单
        /// </summary>
        public static string BuildMenu
        {
            get { return WechatAPI4Public._buildMenu.SetToken(); }
        }

        private static string _deleteMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 删除菜单
        /// </summary>
        public static string DeleteMenu
        {
            get { return WechatAPI4Public._deleteMenu.SetToken(); }
        }

        private static string _getMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
        /// <summary>
        /// 获取菜单
        /// </summary>
        public static string GetMenu
        {
            get { return WechatAPI4Public._getMenu.SetToken(); }
        }
        #endregion


        /// <summary>
        /// 将AccessToken替换到URL地址参数中
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string SetToken(this string url)
        {
            AccessTokenDal tokenDal = new AccessTokenDal();
            return url.Replace("=ACCESS_TOKEN", "=" + tokenDal.GetPublicToken() + "&t=" + DateTime.Now.Ticks.ToString());
        }
    }
}
